<?php
class UsersController extends DomController {
	
	public function users() {
		$this->auth('manageAdminUsers');
		
		$this->view->contents = DomFg::render( $panel );
		
		$post = DomFgPost::getInstance('users')->store('users', array('search') );
		$query = AdminUser::query()->orderBy('t.username');
					
		if( $_GET['delete'] && $item = AdminUser::load( $_GET['delete'] )){
			$item->delete();
		}
		
		if( $post->search ) {
			$query->where('username LIKE \'%'.addslashes($post->search).'%\'');
		}

		function getName( $row ) {
			return $row->firstName.' '.$row->lastName;
		}
		
		function isActive( $row ) {
			if( $row->blocked ) {
				
				$toolTip = DomFgToolTip::c()
					->title('Blokeerimise põhjus')
					->contents($row->blockReason)
					->elementId( 'blockReason_'.$row->id);
				
				return '<b id="blockReason_'.$row->id.'" style="color:red">Blokeeritud</b>';
			} else if( !$row->emailConfirmed )
				return '<b style="color:orange">Kinnitamata</b>';
			else
				return '<b style="color:green">Ok</b>';
		}
		
		$panel = array(
			'title' => 'Kasutajad',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 4,
				'columnWidths' => array(150,60,220),
			),
			'action' => $this->getUrl('users/'),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Lisa uus',
					'link' => $this->getUrl('user/'),
				),
				array(
					'type' => 'textField',
					'value' => $post->search,
					'name' => 'search',
					'title' => 'Otsi',
				),
				array(
					'type' => 'button',
					'title' => 'Otsi',
					'name' => 'doSearch',
				),
				array(
					'type' => 'grid',
					'name' => 'users',
					'pagerResetPostVar' => 'doSearch',
					'source' => new DomArDataSource( $query, 40, users ),
					'colspan' => 4,
					'fields' => array(
						array('key' => 'username', 'title' => 'Kasutajanimi'),
						array('handler'=>'getName', 'title' => 'Nimi'),
						array('key' => 'email', 'title' => 'Email'),
						array('handler' => 'isActive', 'title' => 'Aktiivne'),
						array('type' => 'gridButton', 'value' => 'Change', 'link' => $this->getUrl('user/'), 'title' => 'Valikud','colspan' => 2, 'width'=>50),
						array('type' => 'gridButton', 'value' => 'Delete', 'link' => $this->getUrl('users/&delete='), 'confirmMessage' => 'Are you sure?', 'width'=>50 ),
					),
				),
			),
		);
				
		$this->view->contents = DomFg::render( $panel );		
	}
	
	public function user( $id = NULL ) {
		$this->auth('manageAdminUsers');
		
		$new = $id == 0;
		$item = $new ? new AdminUser() : AdminUser::load($id);
				
		$post = DomFgPost::getInstance();
		
		if( $post->save ) {
			
			$item->username = $post->username;
			if( $post->password1 )
				$item->password = array($post->password1,$post->password2);
			$item->email = $post->email;
			$item->emailConfirmed = (bool)$post->emailConfirmed;
			
			$item->firstName = $post->firstName;
			$item->lastName = $post->lastName;
			
			$item->blocked = (bool)$post->blocked;
			$item->blockReason = $post->blockReason;
			
			$item->rights->set('manageSites', (bool)$post->manageSites);
			$item->rights->set('manageAdminUsers', (bool)$post->manageAdminUsers);
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array('type' => 'ok','colspan' => 2,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'error','colspan' => 2,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}

		$tabPanel = array(
			'type' => 'tabPanel',
			'headerTabs' => true,
			'action' => $this->getUrl('user/'.$id),
			'items' => array(
				$message ,
			),
		);
		
		// --------------------------------------------------------------
		$panel = array(
			'title' => 'Kasutaja',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				array(
					'type' => 'textField',
					'title' => 'Kasutajanimi',
					'value' => $item->username,
					'name' => 'username',
				),
				array(
					'type' => 'password',
					'title' => 'Parool',
					'name' => 'password1',
				),
				array(
					'type' => 'password',
					'title' => 'Parool uuesti',
					'name' => 'password2',
				),
				array(
					'type' => 'textField',
					'title' => 'Email',
					'value' => $item->email,
					'name' => 'email',
				),
				array(
					'type' => 'checkBox',
					'title' => 'Aktiveeritud',
					'checked' => $item->emailConfirmed,
					'name' => 'emailConfirmed',
					'textAfter' => '(Emaili aadress kinnitatud)',
				),
				
				array(
					'type' => 'separator',
					'title' => 'Isik',
					'colspan' => 2,
				),
				
				array(
					'type' => 'textField',
					'title' => 'Eesnimi',
					'value' => $item->firstName,
					'name' => 'firstName',
				),
				array(
					'type' => 'textField',
					'title' => 'Perenimi',
					'value' => $item->lastName,
					'name' => 'lastName',
				),
				
				array(
					'type' => 'separator',
					'title' => 'Blokeerimine',
					'colspan' => 2,
				),
				
				array(
					'type' => 'checkBox',
					'title' => 'Blokeeritud',
					'checked' => $item->blocked,
					'name' => 'blocked',
				),
				array(
					'type' => 'textField',
					'title' => 'Blokeerimise põhjus',
					'value' => $item->blockReason,
					'name' => 'blockReason',
					'width'=> '400px',
				),
				
				
				
			),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
			),
		);
		$tabPanel['items'][] = &$panel;

		// --------------------------------------------------------------
		
		if( $item->id > 0) {
			$rightsPanel = array(
				'title' => 'Õigused',
				'layout' => 'table',
				'layoutConfig' => array(
					'columns' => 2,
					'columnWidths' => array( 150 ),
				),
				'items' => array(
					array(
						'type' => 'checkBox',
						'name' => 'manageSites',
						'title' => 'Luba hallata veebilehti',
						'checked' => $item->rights->isAllowed('manageSites'),
					),
					array(
						'type' => 'checkBox',
						'name' => 'manageAdminUsers',
						'title' => 'Luba hallata administraatoreid',
						'checked' => $item->rights->isAllowed('manageAdminUsers'),
					),
				),
				'buttons' => array(
					array(
						'title' => 'Salvesta',
						'name' => 'save'
					),
				),
			);
			$tabPanel['items'][] = &$rightsPanel;
		}
		// --------------------------------------------------------------
		
		if( $item->id > 0) {
			$statisticsPanel = array(
				'title' => 'Statistika',
				'layout' => 'table',
				'layoutConfig' => array(
					'columns' => 2,
					'columnWidths' => array( 150 ),
				),
				'items' => array(
					array(
						'type' => 'value',
						'title' => 'Registreerus',
						'value' => $item->registered->dateTime('d-m-Y H:i:s'),
					),
					array(
						'type' => 'value',
						'title' => 'Viimati logis sisse',
						'value' => $item->lastLogin->dateTime('d-m-Y H:i:s'),
					),
					array(
						'type' => 'value',
						'title' => 'Viimane tegevus',
						'value' => $item->lastAction->dateTime('d-m-Y H:i:s'),
					),
					
					array(
						'type' => 'value',
						'title' => 'Sisseloginud kordi',
						'value' => DomSql::selectOneQuery('SELECT count(*) as n FROM '.DomUserActionLog::getDbTable().' WHERE user = '.$item->id.' AND userClassName = \''.get_class($item).'\' AND action = 1','n'),
					),
				),
			);
			$tabPanel['items'][] = &$statisticsPanel;
		}
		// --------------------------------------------------------------
		
		$this->view->contents = DomFg::render( $tabPanel );
		
	}
	
	public function error404() {
		$this->loadController('Index')->error404();
	}
	
	public function error403() {
		$this->loadController('Index')->error403();
	}
	
}
?>